package bkt.framework.gui.containers;

import java.util.ArrayList;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import bkt.framework.R;
import android.content.Context;
import android.graphics.Color;
import android.telephony.TelephonyManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class CommonContainer extends RelativeLayout{

	private AdView adView;
	private static final String AD_UNIT_ID = "ca-app-pub-2578495987011609/2304885378";
	
	private LayoutInflater inflater = null;
	private ArrayList<View> objects = new ArrayList<View>();
	private RelativeLayout content = null;
	private LinearLayout adArea = null;
	
	
	public CommonContainer(Context context) {
		super(context);
		this.inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public CommonContainer(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public CommonContainer(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

    @Override
    public void onFinishInflate() {

        getChildsCopy();
        removeAllViews();
        inflateCommons();
        inserChilds();
        objects.clear();

     // Create an ad.
        adView = new AdView(getContext());
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        adView.setBackgroundColor(Color.TRANSPARENT);
        // Add the AdView to the view hierarchy. The view will have no size
        // until the ad is loaded.
        adArea.addView(adView);

        final TelephonyManager tm = (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE);
            String testDeviceId = tm.getDeviceId();
        
        // Create an ad request. Check logcat output for the hashed device ID to
        // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice(testDeviceId)
            .build();

        // Start loading the ad in the background.
        adView.loadAd(adRequest);
        
    }
    

    private void getChildsCopy() {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            objects.add(getChildAt(i));
        }
    }

    private void inflateCommons() {

        this.inflater.inflate(R.layout.common_container, this, true);
        content = (RelativeLayout) findViewById(R.id.common_container_rl_content);
        adArea = (LinearLayout) findViewById(R.id.common_container_ll_adv);
    }

    private void inserChilds() {

        if (objects != null && objects.size() > 0) {
            for (View view : objects) {
                content.addView(view);
            }
        }

    }



	
	
}
